<?php

namespace app\api\extend\ajcaptcha;

/**
 * 滑动验证
 * 依赖于 https://gitee.com/fastknife/aj-captcha
 */
class CaptchaSlide
{
    public static function getCaptcha($config = []) {
        $service = self::_getService($config);
        $data = $service->get();
        if(!$data) {
            return false;
        }
        return $data;
    }

    // 验证 - 主动删除token
    public static function check($token, $pointJson) {
        try {
            $service = self::_getService();
            $service->check($token, $pointJson);
            $service->deleteCacheByToken($token);
        } catch (\Exception $e) {
            \Debug::setDebug($e->getMessage());
            return false;
        }
        return true;
    }

    private static function _getService($config = []) {
        $configFromFile = config('slide');
        if($config) {
            $configFromFile = array_merge($configFromFile, $config);
        }

        $service = new Service($configFromFile);
        return $service;
    }
}